package chapter17;

/**
 * @Auther: Tx
 * @Date: 2022/7/13 15:40
 * 模拟死锁
 */
public class DeadLock {
    public static void main(String[] args) {
        MockDeadLock A = new MockDeadLock(true);
        MockDeadLock B = new MockDeadLock(false);
        A.start();
        B.start();
    }
}

class MockDeadLock extends Thread {
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;

    public MockDeadLock(boolean flag) {
        this.flag = flag;
    }
//同步代码

    @Override
    public void run() {
        if (flag) {
            synchronized (o1) {
                System.out.println("进入1");
                synchronized (o2) {
                    System.out.println("进入2");
                }
            }
        } else {
            synchronized (o2) {
                System.out.println("进入3");
                synchronized (o1) {
                    System.out.println("进入4");
                }
            }

        }
    }
}
